package org.polaris.gift.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import org.polaris.gift.client.AuthClient;
import org.polaris.gift.constants.ResponseCode;
import org.polaris.gift.domain.Employee;
import org.polaris.gift.domain.LoginUser;
import org.polaris.gift.domain.Tenant;
import org.polaris.gift.dto.TenantOccupancyDTO;
import org.polaris.gift.exception.BusinessException;
import org.polaris.gift.exception.PolarisAssert;
import org.polaris.gift.mapper.EmployeeMapper;
import org.polaris.gift.mapper.TenantMapper;
import org.polaris.gift.query.TenantQuery;
import org.polaris.gift.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.polaris.gift.util.AjaxResult;
import org.polaris.gift.util.BaiduAuditUtils;
import org.polaris.gift.util.PageList;
import org.polaris.gift.util.SendMailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author polaris
 * @since 2023-06-24
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private AuthClient authClient;

    @Override
    public PageList<Tenant> advancedAndAssociatedQueryAndPaging(TenantQuery query) {
        Page<Tenant> tenantPage = new Page<>(query.getPage(), query.getRows());
        List<Tenant> data = tenantMapper.advancedAndAssociatedQueryAndPaging(tenantPage,query.getKeyword());
        PageList<Tenant> pageList = new PageList<>(tenantPage.getTotal(), data);
        return pageList;
    }

    @Override
    public AjaxResult tenantOccupancy(TenantOccupancyDTO dto) {

        PolarisAssert.isEq(dto.getPassword(), dto.getConfirmPassword(), ResponseCode.RESPONSE_CODE_400001);

        Map<String, Object> map = BaiduAuditUtils.textCensor(dto.getCompanyName());
        if (!map.get("message").equals("合规")){
            throw new BusinessException("审核失败!");
        }

        //远程调用授权中心添加loginUser返回LoginId
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(UUID.randomUUID().toString());
        loginUser.setTel(dto.getAdminPhone());
        loginUser.setPassword(dto.getPassword());
        loginUser.setEmail(dto.getEmail());
        loginUser.setType(1);
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        if (!ajaxResult.getSuccess())
            return ajaxResult;

        Long loginId = Long.valueOf(ajaxResult
                .getResultObj().toString());
        //租户管理员
        Employee employee = new Employee();
        employee.setUsername(dto.getAdminName());
        employee.setTel(dto.getAdminPhone());
        employee.setPassword(dto.getPassword());
        employee.setEmail(dto.getEmail());
        employee.setLoginId(loginId);
        employee.setInputTime(new Date());
        employee.setState(1);
        employeeMapper.insert(employee);

        //租户
        Tenant tenant = new Tenant();
        tenant.setCompanyName(dto.getCompanyName());
        tenant.setCompanyNum(dto.getCompanyNum());
        tenant.setCompanyNum(dto.getAdminPhone());
        tenant.setAddress(dto.getAddress());
        tenant.setLogo(dto.getLogo());
        tenant.setRegisterTime(new Date());
        tenant.setAdminId(employee.getId());
        tenantMapper.insert(tenant);

        //租户套餐绑定
        tenantMapper.bindMeal(tenant.getId(),dto.getMeal());

/*        new SendMailUtil().send("入住成功通知",
                "入住成功通知","1527138015@qq.com" , null);*/

        return AjaxResult.me();


    }
}
